﻿Imports ADOEntityKYC
Imports ADOEntity
Imports System.Data
Imports System.Data.Linq.SqlClient

Partial Class KYC_chkRiskAllCustomer
    Inherits Web.UI.BasePage

#Region "Valiable"
    Dim ds As DataSet
    Dim KYCEntity As KYCEntities = New KYCEntities
    Dim UserEntity As UserEntities = New UserEntities
    Dim clsDataTable As New clsLINQToDataTable
    Dim convertDate As clsDateFormat
    Dim clsCheckLanguage As clsCheckLanguage
    Dim clsDatabase As clsDatabase
    Dim clsSessionMenuRight As clsSessionMenuRight = New clsSessionMenuRight
    Dim clsMasterDropDown As clsMasterDropDown
#End Region

#Region "Even GridView"

    Dim intBeforeItemIndex As Integer

    Protected Sub grdIndividual_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdIndividual.PageIndexChanging

        grdIndividual.PageIndex = e.NewPageIndex
        grdIndividual.SelectedIndex = -1
        grdIndividual.DataSource = ds.Tables("dsCustomerS")
        grdIndividual.DataBind()

    End Sub
     
    Protected Sub grdIndividual_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdIndividual.SelectedIndexChanged

        If (grdIndividual.SelectedIndex = intBeforeItemIndex) Then
            grdIndividual.SelectedIndex = -1
        End If

    End Sub
    Protected Sub grdIndividual_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdIndividual.SelectedIndexChanging

        intBeforeItemIndex = grdIndividual.SelectedIndex

    End Sub



#End Region


#Region "Even Page"

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        lblDept.Text = Resources.resUser.DepartmentName
        lblBranch.Text = Resources.resUser.BranchName
        lblStatusEtL.Text = "ระดับความเสี่ยง"


    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try
            btnSearch.Attributes.Add("onclick", "NoDoubleSubmit();")

            clsSessionMenuRight = Session("clsMenuRight")

            If Not IsPostBack Then
                chkIsLogin()
                ds = New DataSet()
                resourceSet()
                CreateControlMP()
                loadDropDown()




            Else
                ds = Session("ds")
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try

    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("ds") = ds
    End Sub

#End Region

    Sub resourceSet()

        Dim clsDate As New clsDateFormat
        txtStartDate.Text = clsDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)
        txtEndDate.Text = clsDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)

    End Sub
    Sub CreateControlMP()
        Dim img As Image = CType(Master.FindControl("imgHead"), Image)
        img.ImageUrl = "~/img/Individual.jpg"

        Dim page As Label = CType(Master.FindControl("lblPage"), Label)
        page.Text = "รายงานปริมาณลูกค้าตามระดับความเสี่ยง"
    End Sub

    Private Sub loadDropDown()
        clsMasterDropDown = New clsMasterDropDown
        Try

            clsMasterDropDown.setDeptToDdlSearch(ddlDepartment, _
                                             strDeptCode:=clsSessionMenuRight.getDeptCode, _
                                             strRoleCode:=clsSessionMenuRight.getRoleGroup)
            clsMasterDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=clsSessionMenuRight.getDeptCode, _
                                                      strBranchCode:=clsSessionMenuRight.getBranchCode, _
                                                strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                                strZoneCode:=clsSessionMenuRight.getZoneCode)

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        Try

            IsDataBind()

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub


    Private Sub IsDataBind()
        Dim clsFormatDate As New clsDateFormat
        Try

            If ds.Tables.Contains("dsCustomerS") Then
                ds.Tables.Clear()
            End If

            Dim tblCustomer = From tblCust In KYCEntity.MCUSTOMER
                              Select tblCust.UPDATEDATE, tblCust.UPDATEBY,
            tblCust.CUSTCODE, tblCust.CUSTSEQ, tblCust.BRANCHCODE, tblCust.AMLCDD, tblCust.AMLREASON,
            tblCust.THFIRSTNAME, tblCust.THMIDNAME, tblCust.THLASTNAME, tblCust.CUSTIDTYPE, tblCust.UNIQUEID,
            tblCust.ZONECODE, tblCust.DEPTCODE, tblCust.ALLCODERIGHT

            If Not (ddlDepartment.SelectedValue = "0") Then
                tblCustomer = tblCustomer.Where(Function(cus) cus.DEPTCODE = ddlDepartment.SelectedValue)
            End If

            If Not (ddlBranch.SelectedValue = "0") Then
                tblCustomer = tblCustomer.Where(Function(cus) cus.BRANCHCODE = ddlBranch.SelectedValue)
            End If

            If Not (ddlStatus.SelectedValue = "0") Then
                tblCustomer = tblCustomer.Where(Function(cus) cus.AMLCDD = ddlStatus.SelectedValue)
            End If

            If Not (txtStartDate.Text = "" And txtEndDate.Text = "") Then
                Dim strStartDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtStartDate.Text) & " 00:00:00"
                Dim strEndDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtEndDate.Text) & " 23:59:59"

                tblCustomer = tblCustomer.Where(Function(cus) cus.UPDATEDATE >= strStartDate And cus.UPDATEDATE <= strEndDate)
            End If
   

            'Dim tblcus = tblCustomer.ToList

            Dim tblMcus = From cusjoin In tblCustomer.ToList Join tblUser In UserEntity.SUSER On cusjoin.UPDATEBY.ToString.Trim Equals tblUser.USERNAME.ToString.Trim
                          Order By cusjoin.BRANCHCODE, cusjoin.UPDATEDATE Descending
                           Select cusjoin.UPDATEDATE, cusjoin.CUSTCODE, cusjoin.CUSTSEQ, cusjoin.BRANCHCODE, cusjoin.AMLCDD, cusjoin.AMLREASON,
            cusjoin.THFIRSTNAME, cusjoin.THMIDNAME, cusjoin.THLASTNAME, cusjoin.CUSTIDTYPE, cusjoin.UNIQUEID,
            cusjoin.ZONECODE, cusjoin.DEPTCODE, cusjoin.ALLCODERIGHT, tblUser.ROLECODE

            If (clsSessionMenuRight.getRoleGroup > 89) Then
                'qryUser = qryUser.Where(Function(fn) fn.ROLECODE <= clsSessionMenuRight.getRoleGroup)
            ElseIf (clsSessionMenuRight.getRoleGroup > 79) Then
                tblMcus = tblMcus.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup)
            ElseIf (clsSessionMenuRight.getRoleGroup > 69) Then
                tblMcus = tblMcus.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup)
            ElseIf (clsSessionMenuRight.getRoleGroup > 59) Then
                tblMcus = tblMcus.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup And fn.ZONECODE = clsSessionMenuRight.getZoneCode)
            ElseIf (clsSessionMenuRight.getRoleGroup < 60) Then
                tblMcus = tblMcus.Where(Function(fn) fn.ROLECODE < clsSessionMenuRight.getRoleGroup)
            End If





            ds.Tables.Add(clsDataTable.LINQToDataTable(tblMcus, "dsCustomerS").Copy)
            grdIndividual.DataSource = ds.Tables("dsCustomerS")
            grdIndividual.DataBind()

            If grdIndividual.Rows.Count = 0 Then
                lblMsg.Visible = True
                lblTotal.Text = ""
            Else
                lblMsg.Visible = False
                lblTotal.Text = grdIndividual.Rows.Count
                lblTotal.Text = "จำนวนรายการทั้งหมด " + ds.Tables("dsCustomerS").Rows.Count.ToString + " รายการ "

            End If




        Catch ex As Exception
            Throw ex
        End Try
    End Sub



    Protected Sub ddlDepartment_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDepartment.SelectedIndexChanged
        Try
            Dim clsDropDown As New clsMasterDropDown
            clsSessionMenuRight = Session("ClsMenuRight")
            If Not (ddlDepartment.SelectedValue = "0") Then
                clsDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
            Else
                clsDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
                ddlBranch.SelectedValue = "0"
            End If
            clsDropDown = Nothing
        Catch ex As Exception
            WriteError(ex, "ddlDepartment_SelectedIndexChanged", "checkIndividualSearch")
        End Try



    End Sub


    Protected Sub grdIndividual_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdIndividual.RowDataBound
        If (e.Row.RowType.ToString = "DataRow") Then
            Dim lblUpdateDate As Label = CType(e.Row.FindControl("lblUpdateDate"), Label)
            If Not (lblUpdateDate.Text = "") Then
                convertDate = New clsDateFormat
                lblUpdateDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, lblUpdateDate.Text)
                convertDate = Nothing
            End If
        End If
    End Sub

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        '  Dim param As String = "{ 'ETLSync' : '" & ddlREASONTYPE.SelectedValue & "/" & ddlREASONTYPE.SelectedItem.Text & "'}"
        ' ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", "OpenWindowWithPost('../Form/frmRptDailyETL.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)


        Try

            Dim param As String = "{ 'deptCode' : '" & ddlDepartment.SelectedValue & "/" & ddlDepartment.SelectedItem.Text & "'," _
                             & "'BranchCode' : '" & ddlBranch.SelectedValue & "/" & ddlBranch.SelectedItem.Text & "'," _
                             & "'AMLCDD' : '" & ddlStatus.SelectedValue & "/" & ddlStatus.SelectedItem.Text & "'," _
                             & "'StartDate' : '" & txtStartDate.Text.Trim & "'," _
                             & "'EndDate' : '" & txtEndDate.Text.Trim & "'} "

            ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", "OpenWindowWithPost('../Form/frmChkRiskDetail.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                            UpdatePanel1.GetType(), _
                                                            "clientScript", _
                                                            "alert('" + ex.Message.Replace("'", "") + "')", _
                                                            True)
        End Try
    End Sub
End Class
